#!/bin/bash
# Startup script for syspart
#
# chkconfig: 12345 90 99
# description: Partition system resources

# Source function library.
. /etc/rc.d/init.d/functions

# Get config.
[ -f /etc/sysconfig/syspart ] || exit 5
. /etc/sysconfig/syspart

# Main syspart tool
SYSPART=/usr/sbin/syspart

# Init cpusets
CPUSETDIR=/sys/fs/cgroup/cpuset
if [ ! -d $CPUSETDIR -o ! -f $CPUSETDIR/cpuset.cpus ]; then
	echo $CPUSETDIR does not exist or cpuset fs is not mounted
	exit 1
fi
 
# Compute masks based on the config
# All cpus in the system
ALL_CPUS_MASK=`cat $CPUSETDIR/cpuset.cpus | bitops --fmt=lX`

# Backwards compatibility
[ "$GPP_CPUS" = "" ] && GPP_CPUS=$PAR0_CPUS
[ "$GPP_MEMS" = "" ] && GPP_MEMS=$PAR0_MEMS

# GPP cpus and memory nodes
GPP_CPUS_MASK=`echo $GPP_CPUS | bitops --fmt=lX`
GPP_CPUS_LIST=$GPP_CPUS
GPP_MEMS_LIST=$GPP_MEMS

# ISP cpus and memory nodes
[ "$ISP_CPUS" = "" ] && ISP_CPUS_MASK=`echo $ALL_CPUS_MASK | bitops --andnot $GPP_CPUS_MASK`
[ "$ISP_MEMS" = "" ] && ISP_MEMS=$GPP_MEMS
ISP_CPUS_LIST=`echo $ISP_CPUS_MASK | bitops --fmt=xL`
ISP_MEMS_LIST=$ISP_MEMS

start()
{
 	echo -n $"Starting syspart: "

	$SYSPART --isp-cpus $ISP_CPUS_LIST --isp-mems $ISP_MEMS_LIST --gpp-mems $GPP_MEMS_LIST --create

	success $"syspart"
	echo 

	RETVAL=0
	return $RETVAL
}

stop()
{
 	echo -n $"Stopping syspart: "

	$SYSPART --destroy

	success $"syspart"
	echo

	RETVAL=0
	return $RETVAL 
}

status()
{
	$SYSPART --isp-cpus $ISP_CPUS_LIST --isp-mems $ISP_MEMS_LIST --gpp-mems $GPP_MEMS_LIST --check
}

case "$1" in
  start)
    start
    ;;

  stop)
    stop
    ;;

  status)
    status
    ;;

  restart)
    stop
    start
    ;;

  condrestart)
    ;;

  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 3
    ;;
esac

exit $RETVAL
